package legumes.server.usl.services;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;

import jxl.Workbook;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import legumes.shared.domain.Order;

/**
 * Generates an excel document.
 * 
 * @author cpipo
 * 
 */
public class ToExcellGae {

	private static final int NAME_COLUMN = 0;
	private static final int MENU_COLUMN = 1;
	private static final int PLUS_COLUMN = 2;
	private static final int MINUS_COLUMN = 3;

	
	public static ByteArrayOutputStream toExcell(Collection<Order> orders_, OutputStream out_)
		throws IOException, WriteException {
		 
	  // Stream containing excel data
	  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	 
	  // Create Excel WorkBook and Sheet
	  WritableWorkbook workBook = Workbook.createWorkbook(outputStream);
	  WritableSheet sheet = workBook.createSheet("User List", 0);
	 
	  // Generates Headers Cells
	  WritableFont headerFont = new WritableFont(WritableFont.TAHOMA, 12, WritableFont.BOLD);
	  WritableCellFormat headerCellFormat = new WritableCellFormat(headerFont);
	  headerCellFormat.setBackground(Colour.PALE_BLUE);
	  sheet.addCell(new Label(1, 1, "LastName", headerCellFormat));
	  sheet.addCell(new Label(2, 1, "FirstName", headerCellFormat));
	 
	  // Generates Data Cells
	  WritableFont dataFont = new WritableFont(WritableFont.TAHOMA, 12);
	  WritableCellFormat dataCellFormat = new WritableCellFormat(dataFont);
	  int currentRow = 2;
//	  for (User user : getUsers()) {
//	    sheet.addCell(new Label(1, currentRow, user.getLastName(),dataCellFormat));
//	    sheet.addCell(new Label(2, currentRow, user.getFirstName(),dataCellFormat));
//	    currentRow++;
//	  }
	 
	  // Write & Close Excel WorkBook
	  workBook.write();
	  workBook.close();
	 
	  return outputStream;
	}

	
//	
//	public static void toExcell(Collection<Order> orders_, OutputStream out_, String toto)
//			throws IOException {
//
//		
//		
//		HSSFWorkbook workBook = new HSSFWorkbook();
//		HSSFSheet sheet = workBook.createSheet();
//		HSSFRow row = null;
//		HSSFCell cell = null;
//		StringBuilder sb;
//
//		HSSFCellStyle nameCellStyle = workBook.createCellStyle();
//		nameCellStyle.setWrapText(true);
//		nameCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//		nameCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//		nameCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//		nameCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
//		nameCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
//
//		HSSFCellStyle menuCellStyle = workBook.createCellStyle();
//		menuCellStyle.setWrapText(true);
//		menuCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//		menuCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//		menuCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//		menuCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
//		menuCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//		menuCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
//
//		HSSFCellStyle plusMinusCellStyle = workBook.createCellStyle();
//		plusMinusCellStyle.setWrapText(true);
//		plusMinusCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//		plusMinusCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//		plusMinusCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//		plusMinusCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
//		plusMinusCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP);
//
//		int rowNum = 0;
//
//		// HEADERS
//		HSSFCellStyle headersCellStyle = workBook.createCellStyle();
//		headersCellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//		headersCellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
//		headersCellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//		headersCellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
//		headersCellStyle.setFillBackgroundColor(HSSFColor.LIGHT_GREEN.index);
//		headersCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//		HSSFFont headerFont = workBook.createFont();
//		headerFont.setFontHeightInPoints((short) 12);
//		headerFont.setFontName("Arial");
//		headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//		headersCellStyle.setFont(headerFont);
//
//		row = sheet.createRow(rowNum++);
//		cell = row.createCell(NAME_COLUMN);
//		cell.setCellStyle(headersCellStyle);
//		cell.setCellValue(new HSSFRichTextString("Nom"));
//		cell = row.createCell(MENU_COLUMN);
//		cell.setCellStyle(headersCellStyle);
//		cell.setCellValue(new HSSFRichTextString("Nb Panier"));
//		cell = row.createCell(PLUS_COLUMN);
//		cell.setCellStyle(headersCellStyle);
//		cell.setCellValue(new HSSFRichTextString("+"));
//		cell = row.createCell(MINUS_COLUMN);
//		cell.setCellStyle(headersCellStyle);
//		cell.setCellValue(new HSSFRichTextString("-"));
//
//		// DATA
//		for (Order order : orders_) {
//
//			for (OrderPart orderPart : order.getOrderParts()) {
//
//				row = sheet.createRow(rowNum++);
//
//				// NAME
//				cell = row.createCell(NAME_COLUMN);
//				cell.setCellStyle(nameCellStyle);
//				cell.setCellValue(new HSSFRichTextString(order
//						.getCustomerName()));
//
//				// Panier
//				cell = row.createCell(MENU_COLUMN);
//				cell.setCellStyle(menuCellStyle);
//				cell.setCellValue(new HSSFRichTextString(orderPart
//						.getOrderPartMenuItem().getMenuCountAsEnum()
//						.getDisplayName()));
//
//				// +
//				cell = row.createCell(PLUS_COLUMN);
//				cell.setCellStyle(plusMinusCellStyle);
//				sb = new StringBuilder();
//				for (int i = 0; i < orderPart.getOrderPartProductItems().size(); i++) {
//					OrderPartProductItem oppi = orderPart
//							.getOrderPartProductItems().get(i);
//					sb.append(oppi.getQuantity()).append(" ").append(
//							oppi.getOnSaleProduct().getProduct().getName());
//					if (i != orderPart.getOrderPartProductItems().size() - 1) {
//						sb.append(" - ");
//					}
//				}
//				cell.setCellValue(new HSSFRichTextString(sb.toString()));
//
//				// -
//				cell = row.createCell(MINUS_COLUMN);
//				cell.setCellStyle(plusMinusCellStyle);
//				sb = new StringBuilder();
//				for (int i = 0; i < orderPart.getOrderPartMenuItem()
//						.getRemovedMenuItems().size(); i++) {
//					MenuItem mi = orderPart.getOrderPartMenuItem()
//							.getRemovedMenuItems().get(i);
//					if (i != orderPart.getOrderPartMenuItem()
//							.getRemovedMenuItems().size() - 1) {
//						sb.append(mi.getArticleName()).append(" - ");
//					}
//				}
//				cell.setCellValue(new HSSFRichTextString(sb.toString()));
//			}
//		}
//
//		sheet.autoSizeColumn((short) NAME_COLUMN);
//		sheet.setColumnWidth(MENU_COLUMN, 2900);
//		sheet.setColumnWidth(PLUS_COLUMN, 10000);
//		sheet.setColumnWidth(MINUS_COLUMN, 10000);
//
//		workBook.write(out_);
//
//	}
}
